package com.hpe.fastfood.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.hpe.fastfood.mapper.BussinessMapper;
import com.hpe.fastfood.pojo.Bussiness;
import com.hpe.fastfood.service.BussinessService;
import com.hpe.fastfood.util.Page;

@Service
public class BussinessServiceImpl implements BussinessService {

	@Autowired
	private BussinessMapper bussinessMapper;

	@Override
	public Page selectBussiness(Page page, String bussinessName) throws RuntimeException {
		// TODO Auto-generated method stub
		int start = (page.getCurPage() - 1) * page.getPageNumber();// 起始页
		List<Bussiness> course = bussinessMapper.selectBussiness(start, page.getPageNumber(), bussinessName);// 分页查询记录
		// 查询一共多少条记录
		int rows = bussinessMapper.selectCount(bussinessName);
		// 计算一共多少页
		int totalPage = rows % page.getPageNumber() == 0 ? rows / page.getPageNumber()
				: rows / page.getPageNumber() + 1;
		// 封装数据到page进行放回
		page.setDate(course);
		page.setRows(rows);
		page.setTotalPage(totalPage);
		return page;
	}

	@Override
	public void changeBussiness(Bussiness bussiness) {
		// TODO Auto-generated method stub
		bussinessMapper.changeBussiness(bussiness);
	}

	@Override
	public void updateBussiness(Bussiness bussiness) throws RuntimeException {
		// TODO Auto-generated method stub
		bussinessMapper.updateBussiness(bussiness);
	}

	@Override
	public Bussiness selectBussinessById(int bussinessId) throws RuntimeException {
		// TODO Auto-generated method stub
		return bussinessMapper.selectBussinessById(bussinessId);

	}

}
